package com.eastidea.live.controller.mobi;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eastidea.live.dao.CompanyDao;
import com.eastidea.live.dao.UserDao;
import com.eastidea.live.entity.AppCompany;
import com.eastidea.live.entity.AppUser;
import com.eastidea.live.service.UserService;
import com.eastidea.live.util.Constant;
import com.eastidea.live.util.StringUtil;

@Controller
@RequestMapping(value = Constant.API_REST_V1 + "/user/")
public class UserController {

	@Resource
	UserService userService;

	@Resource
	UserDao userDao;
	
	@Resource
	CompanyDao companyDao;
	
	@RequestMapping(value = "signon")
	public Map<String, Object> signon(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		Map<String, Object> res = new HashMap<String, Object>();
		
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rememberMe = request.getParameter("rememberMe");		
		
		if (StringUtil.IsEmpty(email) || StringUtil.IsEmpty(password)) {
			res.put("success", false);
			return res;
		}

		DetachedCriteria dc = DetachedCriteria.forClass(AppUser.class);
		dc.add(Restrictions.eq("email", email));
		dc.add(Restrictions.eq("passwd", password));
		
		List<AppUser> ls = userDao.findByCriteria(dc);
		AppUser user = null;
		String token = null;
		if (ls.size() > 0) {
			user = ls.get(0);

			if (StringUtil.isNotEmpty(rememberMe) && rememberMe.equals("on")) {
				token = StringUtil.RandomString(24);
				user.setToken(token);
			}
			
			userDao.update(user);

			request.getSession().setAttribute("user", user);

			res.put("success", true);
			res.put("email", email);
			res.put("token", token);
			return res;
		} else {
			res.put("success", false);
			return res;
		}		
	}

	@RequestMapping(value = "signonWithToken")
	public Map<String, Object> signonWithToken(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		
		Map<String, Object> res = new HashMap<String, Object>();
		
		String token = request.getParameter("token");		
		if (StringUtil.IsEmpty(token)) {
			res.put("success", false);
			return res;
		}

		DetachedCriteria dc = DetachedCriteria.forClass(AppUser.class);
		dc.add(Restrictions.eq("token", token));
		List<AppUser> ls = userDao.findByCriteria(dc);
		AppUser user = null;
		if (ls.size() > 0) {
			user = ls.get(0);
			request.getSession().setAttribute("user", user);

			res.put("success", true);
			res.put("email", user.getEmail());
			res.put("token", token);
			return res;
		} else {
			res.put("success", false);
			return res;
		}
	}
	
	@RequestMapping(value = "signout")
	public Map<String, Object> signout(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		Map<String, Object> res = new HashMap<String, Object>();
		
		request.getSession().removeAttribute("user");
		request.getSession().invalidate();
		
		res.put("success", true);
		return res;
	}

	@RequestMapping(value = "signup")
	public Map<String, Object> signup(HttpServletRequest request,
			ModelMap modelMap) throws IOException {
		Map<String, Object> res = new HashMap<String, Object>();
		
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		
		if (StringUtil.IsEmpty(email) || StringUtil.IsEmpty(password)) {
			res.put("success", false);
			return res;
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(AppUser.class);
		dc.add(Restrictions.eq("email", email));
		List<AppUser> ls = userDao.findByCriteria(dc);
		if (ls.size() > 0) {
			res.put("success", false);
			res.put("msg", "");
			return res;
		}
		
		AppUser user = new AppUser();
		user.setEmail(email);
		user.setPasswd(password);					
		user.setStatus(1);
		user.setUpdateTime(new Date());
		String token = StringUtil.RandomString(24);
		user.setToken(token);
		
		AppCompany company = new AppCompany();
		company.setName(email);
		company.setStatus(1);
		companyDao.save(company);
		
		user.setAppCompany(company);
		userDao.save(user);
		
		request.getSession().setAttribute("user", user);
		
		res.put("success", true);
		res.put("email", email);
		res.put("token", token);
		return res;
	}

}
